<template>
  <span>
    <el-descriptions title="禁毒工作情况记录" border :column="1">
    <template slot="extra">
      <el-button type="text" size="small" @click="handleUpdate(gzrz)">编辑</el-button>
      <el-button type="text" size="small" @click="handleDelete(gzrz)" style="color: red;">删除</el-button>
    </template>
    <el-descriptions-item label="日期">{{ gzrz.date }}</el-descriptions-item>
    <el-descriptions-item label="人员">{{ gzrz.memberInfo }}</el-descriptions-item>
    <el-descriptions-item :labelStyle="{width: '100px'}" label="工作内容">{{ gzrz.content }}</el-descriptions-item>
    <el-descriptions-item label="图片">
      <image-upload v-model="gzrz.images" readonly/>
    </el-descriptions-item>
  </el-descriptions>
  <el-dialog title="工作日志编辑" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="日期" prop="date">
        <el-date-picker clearable
                        v-model="form.date"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工作人员" prop="memberId">
        <user-member-select v-model="form.memberId" placeholder="请选择工作人员"/>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="form.content" type="textarea"/>
      </el-form-item>
      <el-form-item label="图片" prop="images">
        <image-upload v-model="form.images"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
  </span>
</template>

<script>
import {delGzrz, getGzrz, updateGzrz} from "@/api/duty/gzrz";

export default {
  name: "rzDetail",
  props: {
    gzrz: Object
  },
  data() {
    return {
      buttonLoading: false,
      form: {},
      open: false,
      rules: {
        gzrzId: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        memberId: [
          { required: true, message: "工作人员不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        images: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
      }
    }
  },
  methods: {
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const gzrzId = row.gzrzId
      getGzrz(gzrzId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工作日志";
      });
    },
    reset() {
      this.form = {
        gzrzId: undefined,
        date: undefined,
        memberId: undefined,
        content: undefined,
        images: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          updateGzrz(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.$emit('rz-updated');
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$emit('rz-delete', row)
    }
  }
}
</script>

<style scoped>

</style>
